//
//  MainVC.swift
//  LeGou
//
//  Created by lxm on 2022/7/23.
//

import UIKit

class MainVC: UITabBarController, UITabBarControllerDelegate {
    //MARK: --------------------属性--------------------
    let disposeBag = DisposeBag()
    
    static let CENTER_NAME = "乐购竞购"
    //MARK: --------------------UI控件--------------------
    let home = HomeVC_V2()
    let mine = MineVC()
    let tabbbar = LGTabbar()
    //MARK: --------------------生命周期--------------------
    override func viewDidLoad() {
        super.viewDidLoad()
        setTabbar()
        setUI()
        bindAction()
    }
    
    //MARK: --------------------UI--------------------
    func setUI() {
        self.delegate = self
        // 首页
        if let home = router.viewController(for: "xm://main/Home/index") {
            addController(vc: home, title: "首页", defImg: "home_u", selectImg: "home_s")
        }
  
        // vip mall
        if let vc = router.viewController(for: "xm://main/VIPMall/index", context: nil) {
            addController(vc: vc, title: "创客优选", defImg: "vip_mall", selectImg: "vip_mall_s")
        }
        
        // vip mall
        if let vc = router.viewController(for: "xm://main/Auction/index", context: nil), !AppHelper.appVerify() {
            addController(vc: vc, title: MainVC.CENTER_NAME, defImg: "lg_sc", selectImg: "lg_sc_s")
        }
        
        // vip mall
        if let vc = router.viewController(for: "xm://main/LGMall/index", context: nil) {
            addController(vc: vc, title: "乐购商城", defImg: "lg_sc", selectImg: "lg_sc_s")
        }
        
        // 我的
        if let vc = router.viewController(for: "xm://main/Mine/index", context: nil) {
            addController(vc: vc, title: "我的", defImg: "mine_u", selectImg: "mine_s")
        }
        UITabBar.appearance().isTranslucent = false
        tabBar.backgroundImage = UIImage.init()
//        tabBar.shadowImage = UIImage.init()
        tabBar.unselectedItemTintColor = "#333333".zm.hex()
        tabBar.tintColor = "#DE3F26".zm.hex()
    }
    
    func setTabbar() {
        tabbbar.delegate = self
        setValue(tabbbar, forKey: "tabBar")
//        tabbbar.mallButton.addTarget(self, action: #selector(centerButtonClick(sender:)), for: .touchUpInside)
     
    }
    
    @objc func centerButtonClick(sender : UIButton) {
        sender.isSelected = true
        self.selectedIndex = 1
    }

    
    //MARK: --------------------methods--------------------
    func bindAction() {
        tabbbar.centerClick = { [weak self] in
            if LoginService.shared.checkLogin() == false {return}
            guard let self = self else {return}
            self.selectedIndex = 2
        }
    }

    func addController(vc: UIViewController, title: String, defImg: String, selectImg: String) -> Void {
        vc.tabBarItem.title = title
        vc.tabBarItem.image = UIImage.init(named: defImg)?.withRenderingMode(.alwaysOriginal)
        vc.tabBarItem.selectedImage = UIImage.init(named: selectImg)?.withRenderingMode(.alwaysOriginal)
        let font: UIFont = UIFont.zm.fontRegular(size: 11)
        vc.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : "#333333".zm.hex(), NSAttributedString.Key.font : font], for: .normal)
        vc.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : "#DE3F26".zm.hex(), NSAttributedString.Key.font : font], for: .selected)
        let nav = HBDNavigationController(rootViewController: vc)
        addChild(nav)
    }
    
    //MARK: --------------------delegate--------------------
    
    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        if let nav = viewController as? UINavigationController, nav.children.first is MineVC {
            // MineVC
           return LoginService.shared.checkLogin()
        }
        return true
    }
    
    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
        tabbbar.mallButton.isSelected = self.selectedIndex == 1
    }
}
